﻿using System.Collections.Generic;
using System.Globalization;
using System.Text.RegularExpressions;
using System.Windows.Controls;
using DecodingSharePoint.SharePoint;

namespace DecodingSharePoint.System.Windows.Controls
{
    public class DS_Regex_ValidationRule : ValidationRule
    {
        private static Dictionary<string, string> StandardRegularExpressions = new Dictionary<string, string>()
                                                                                {
                                                                                    { DS_Regex_Common.RegexType.Email, @"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$"},
                                                                                    { DS_Regex_Common.RegexType.SSN, @"^(?!000)([0-6]\d{2}|7([0-6]\d|7[012]))([ -]?)(?!00)\d\d\3(?!0000)\d{4}$"},
                                                                                    { DS_Regex_Common.RegexType.PhoneNumbers, @"^\(\d{3}\) ?\d{3}( |-)?\d{4}|^\d{3}( |-)?\d{3}( |-)?\d{4}$"},
                                                                                    { DS_Regex_Common.RegexType.IpAddress, @"^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$"},
                                                                                    { DS_Regex_Common.RegexType.Alphanumeric, @"^[ \w]+$"}
                                                                                };

        private static Dictionary<string, string> StandardErrorMessage = new Dictionary<string, string>()
                                                                         {
                                                                             { DS_Regex_Common.RegexType.Email, "Please enter valid email address"},
                                                                             { DS_Regex_Common.RegexType.SSN, "Please enter valid SSN number" },
                                                                             { DS_Regex_Common.RegexType.PhoneNumbers,"Please enter valid phone number"},
                                                                             { DS_Regex_Common.RegexType.IpAddress, "Please enter valid IP address"},
                                                                             { DS_Regex_Common.RegexType.Alphanumeric, "Please enter valid input"}
                                                                         };

        public override ValidationResult Validate(object value, CultureInfo cultureInfo)
        {
            RegexDTO inputValue = (RegexDTO)value;
            bool result = string.IsNullOrEmpty(inputValue.Value) ? true : IsMatchedByRegex(inputValue);
            if (result != true)
            {
                if (inputValue.RegexType != DS_Regex_Common.RegexType.Other)
                {
                    return new ValidationResult(false, StandardErrorMessage[inputValue.RegexType]);
                }
                return new ValidationResult(false, inputValue.ErrorMessage);
            }
            else
            {
                return new ValidationResult(true, string.Empty);
            }
        }

        public static bool IsMatchedByRegex(RegexDTO inputValue)
        {
            string regexToValidate = string.Empty;
            if (inputValue.RegexType != DS_Regex_Common.RegexType.Other)
            {
                regexToValidate = StandardRegularExpressions[inputValue.RegexType];
            }
            else
            {
                regexToValidate = inputValue.OtherRegex;
            }

            Regex re = new Regex(regexToValidate);
            if (re.IsMatch(inputValue.Value))
                return (true);
            else
                return (false);
        }
    }
}
